<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜单名称" prop="groupName">
        <el-input v-model="queryParams.groupName" placeholder="请输入菜单名称" clearable size="mini"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddParent"
                   v-hasPermi="['system:group:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['system:group:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['system:group:remove']">删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupList" row-key="id"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="菜单名称" align="center" prop="groupName"/>
      <el-table-column label="前置logo" align="center">
        <template slot-scope="scope">
          <img :src="scope.row.logoPath" class="el-avatar">
        </template>
      </el-table-column>
      <el-table-column label="层级" align="center" prop="level" width="250">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.level === 0" style="cursor: pointer" :type="'danger'">菜单</el-tag>
          <el-tag v-if="scope.row.level === 1" style="cursor: pointer" :type="'success'">类目</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column label="父层级id" align="center" prop="parentId"/>-->
      <el-table-column label="状态" align="center" prop="state" width="250">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'danger'">禁用</el-tag>
          <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="'success'">启用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAdd(scope.row)" v-if="scope.row.level==0"
                     v-hasPermi="['system:group:edit']">添加子类目
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateParent(scope.row)" v-if="scope.row.level==0"
                     v-hasPermi="['system:group:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-if="scope.row.level==1"
                     v-hasPermi="['system:group:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-user-solid" v-if="scope.row.level==1">
            <router-link :to="'/property/notice?noticeGroupId=' + scope.row.id + '&noticeType=3'" class="link-type">
              公告
            </router-link>
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:group:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    <pagination v-show="total > 0"-->
    <!--                :total="total"-->
    <!--                :page.sync="queryParams.pageNum"-->
    <!--                :limit.sync="queryParams.pageSize"-->
    <!--                @pagination="getList"/>-->

    <!-- 添加或修改公告公示类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">

        <el-form-item label="所属父层级" prop="parentId">
          <el-select v-model="form.parentId" placeholder="请选择父层级" style="width:100%;" size="mini" filterable
                     clearable>
            <el-option v-for="parent in parentList"
                       :key="parent.id"
                       :label="parent.groupName"
                       :value="parent.id"/>
          </el-select>
        </el-form-item>

        <el-form-item label="菜单名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入菜单名称" size="mini"/>
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="0" :label="0">停用</el-radio>
            <el-radio :value="1" :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改公告公示类型对话框 -->
    <el-dialog :title="title" :visible.sync="openParent" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="95px">

        <el-form-item label="菜单名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入菜单名称" size="mini"/>
        </el-form-item>

        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state" size="mini">
            <el-radio :value="0" :label="0">停用</el-radio>
            <el-radio :value="1" :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>


        <el-form-item label="前置LOGO" prop="logo" style="width:95%;">
          <single-pic v-model="form.logoPath" type="image" @handleChecked="chooseImg" :uploadType="8"
                      :num="1" :width="80" :height="80" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listGroup, getGroup, delGroup, addGroup, updateGroup, listParentGroup} from "../../../api/system/noticeGroup";
import singlePic from '@/components/SingleMaterial'
export default {
  name: "Group",
  components: {
    singlePic,
    Treeselect
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告公示类型表格数据
      groupList: [],
      parentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openParent: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList();
    this.getParentList();
  },
  methods: {
    /** 查询公告公示类型列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        // this.groupList = response.rows;
        console.log(1)
        console.log(response.data)
        this.groupList = this.handleTree(response.data, "id", "parentId");
        // this.total = response.total;
        console.log(2)
        console.log(this.groupList)
        this.loading = false;
      });
    },

    getParentList() {
      listParentGroup(this.queryParams).then(response => {
        console.log(response.data)
        this.parentList = response.data;
        console.log(this.parentList)
        this.loading = false;
      });
    },

    chooseImg(data){
      if(data.length > 0) {
        // this.form.shopLogoPath = data[0].imgUrl;
        this.form.logoPath = data[0].imgUrl;
        this.form.logo = data[0].url;
      }
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.openParent = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        groupName: undefined,
        level: 1,
        parentId: undefined,
        state: undefined,
        logo: undefined,
      }
      this.resetForm("form");
    },
    // 表单重置
    resetParent() {
      this.form = {
        id: undefined,
        groupName: undefined,
        level: 0,
        parentId: 0,
        state: undefined,
        logo: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getParentList()
      this.form.parentId = row.id
      this.open = true;
      this.title = "添加公告公示类型";
    },
    /** 新增按钮操作 */
    handleAddParent() {
      this.resetParent();
      this.getParentList()
      this.openParent = true;
      this.title = "添加公告公示类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getGroup(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改公告公示类型";
      });
    },
    handleUpdateParent(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getGroup(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.openParent = true;
        this.title = "修改公告公示类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGroup(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.openParent = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGroup(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.openParent = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除公告公示类型编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delGroup(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },

  }
};
</script>
